package com.hmall.search.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.search.pojo.Item;
import com.hmall.search.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> pageItem(Integer page,Integer size){
        return itemService.pageItem(page,size);
    }

    /**
     * 根据id找商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getItemById(@PathVariable Long id){
        return itemService.getItemById(id);
    }

    /**
     * 添加商品
     * @param item
     */
    @PostMapping
    public void addItem(@RequestBody Item item){
        itemService.addItem(item);
    }

    /**
     * 上架下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void updateItemStatus(@PathVariable Long id,@PathVariable Integer status){
        itemService.updateItemStatus(id,status);
    }

    /**
     * 根据id修改商品
     * @param item
     */
    @PutMapping
    public void updateItemById(@RequestBody Item item){
        itemService.updateItemById(item);
    }


    /**
     * 删除商品
     * @param id
     */
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id){
        itemService.deleteById(id);
    }

    @DeleteMapping("/stock/{itemId}/{num}")
    public void deleteStock(@PathVariable("itemId")Long id ,@PathVariable("num")Integer num){
        itemService.deleteStock(id,num);
    }

    @GetMapping("/stock/{itemId}/{num}")
    public void addStock(@PathVariable("itemId")Long itemId,@PathVariable("num") Integer num){
        itemService.addStock(itemId,num);
    }
}
